package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.mapper.Condition;
import io.renren.common.annotation.WebApi;
import io.renren.common.response.BaseRequest;
import io.renren.common.response.Response;
import io.renren.common.utils.R;
import io.renren.modules.app.dto.InOrderDTO;
import io.renren.modules.app.enums.InOrderStatusEnum;
import io.renren.modules.app.enums.OutOrderStatusEnum;
import io.renren.modules.app.reponse.OutOrderRes;
import io.renren.modules.app.request.intoorder.DetailInOrderReq;
import io.renren.modules.app.request.intoorder.ListInOrderReq;
import io.renren.modules.app.service.ApiService;
import io.renren.modules.generator.entity.InOrderEntity;
import io.renren.modules.generator.entity.InOrderItemEntity;
import io.renren.modules.generator.service.InOrderItemService;
import io.renren.modules.generator.service.InOrderService;
import io.renren.modules.generator.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * APP测试接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/api")
@Api(
        "入库订单接口"
)
public class IntoOrderController {

//    @Login
//    @GetMapping("userInfo")
//    @ApiOperation("获取用户信息")
//    public R userInfo(@LoginUser UserEntity user) {
//        return R.ok().put("user", user);
//    }
//
//    @Login
//    @GetMapping("userId")
//    @ApiOperation("获取用户ID")
//    public Response userInfo(@RequestAttribute("userId") Integer userId, @LoginUser UserEntity userEntity) {
//        return Response.ok(userEntity.getPassword());
//    }


    @Autowired
    InOrderService inOrderService;

    @Autowired
    InOrderItemService inOrderItemService;


    @GetMapping("getIorderNo")
    @ApiOperation("获取入库单订单号")
    public R getOorderNo() {
        StringBuffer prefix = new StringBuffer("I");
        LocalDateTime ldt = LocalDateTime.now();
        prefix.append(ldt.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")));
        return R.ok().put("data", prefix.toString());
    }

    @Autowired
    ItemService itemService;


    @GetMapping("getInOrder")
    @ApiOperation("入库单详情")
    public R getInOrder(@RequestParam("id") String id) {
        OutOrderRes response = new OutOrderRes();
        if (StringUtils.isNotEmpty(id)) {
            InOrderEntity order = inOrderService.selectById(id);
            if (order != null) {
                response.setCreateTime(order.getCreateTime());
                response.setCreateUser(order.getCreateUser());
                response.setOrderNo(order.getOrderNo());
                response.setStatus(order.getStatus());
                response.setStatusStr(OutOrderStatusEnum.getTextByValue(order.getStatus()));
                List<InOrderItemEntity> items = inOrderItemService.selectList(Condition.create().eq("order_no", order.getOrderNo()));
                List<OutOrderRes.OutOrderItem> orderItems = new ArrayList<>();
                items.forEach(x -> {
                    OutOrderRes.OutOrderItem item = new OutOrderRes.OutOrderItem();
                    BeanUtils.copyProperties(x, item);
                    item.setName(itemService.selectById(x.getItemId()).getName());
                    orderItems.add(item);
                });
                response.setOrderItems(orderItems);
            }
        }
        return R.ok().put("data", response);
    }

    @WebApi
    @PostMapping("getwarehousingtotal")
    @ApiOperation("获取WMS入库单的总数量")
    public Response getwarehousingtotal(@RequestBody BaseRequest request) {
        return Response.ok(inOrderService.selectCount(Condition.create().eq("status", InOrderStatusEnum.初始化.getValue())));
    }

    @Autowired
    ApiService apiService;

    @WebApi
    @PostMapping("getwarehousinglist")
    @ApiOperation("获取入库单列表")
    public Response getwarehousinglist(@RequestBody ListInOrderReq request) {

        return Response.ok(apiService.listInOrder(request));
    }


    @WebApi
    @PostMapping("getwarehousingbycode")
    @ApiOperation("根据单号获取入库单详情")
    public Response getwarehousingbycode(@RequestBody DetailInOrderReq request) {
        InOrderDTO response = new InOrderDTO();
        if (StringUtils.isNotEmpty(request.getCode())) {
            List<InOrderEntity> res = inOrderService.selectList(Condition.create().eq("order_no", request.getCode()).orderBy("id", false));
            if (res != null && res.size() > 0) {
                InOrderEntity one = res.get(0);
                response.setCode(one.getOrderNo());
                response.setCreatedate(one.getCreateTime());
                response.setPositionsum("");
                response.setStatus(one.getStatus());
                response.setStatusStr(InOrderStatusEnum.getTextByValue(one.getStatus()));
                List<InOrderDTO.InOrderItemDTO> haha = new ArrayList<>();
                List<InOrderItemEntity> items = inOrderItemService.selectList(Condition.create().eq("order_no", one.getOrderNo()));
                items.stream().forEach(x->{
                    InOrderDTO.InOrderItemDTO vo = new InOrderDTO.InOrderItemDTO();
                    BeanUtils.copyProperties(x, vo);
                    vo.setItemGuid(x.getItemGuid() + "-" + x.getBatchNo());
                    vo.setItemId(vo.getItemGuid());
                    haha.add(vo);
                });
                response.setItems(haha);


            }
        }
        return Response.ok(response);
    }

//
//    @WebApi
//    @PostMapping("uploadwarehousingresult")
//    @ApiOperation("上传入库拣货结果")
//    public Response uploadwarehousingresult(@RequestBody UpdateInOrderReq request) {
//        if (StringUtils.isNotEmpty(request.getCode())) {
//            apiService.updateInOrder(request);
//        } else {
//            return Response.fail("单号不能为空");
//        }
//        return Response.ok("接口迁移，请统一使用updateStep接口");
//    }


}
